package ipmsg.gui;

import javax.swing.GroupLayout;

public class Blacklist extends javax.swing.JFrame {
  private javax.swing.JList guiBlacklist;
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JButton jButton3;
  private javax.swing.JButton jButton4;
  private javax.swing.JButton jButton5;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JPanel jPanel3;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextField jTextField1;
  private javax.swing.JLabel warning;
  
  public Blacklist() {
    ipmsg.etc.GlobalVar.WINDOW_REG.add("blacklist");
    initComponents();
    setTitle("黑名单");
    refreshGui("当前共有 " + ipmsg.etc.GlobalVar.BLACK_LIST.size() + " 个黑名单");
  }
  







  private void initComponents()
  {
    this.jPanel1 = new javax.swing.JPanel();
    this.jScrollPane1 = new javax.swing.JScrollPane();
    this.guiBlacklist = new javax.swing.JList();
    this.jPanel2 = new javax.swing.JPanel();
    this.jTextField1 = new javax.swing.JTextField();
    this.jButton1 = new javax.swing.JButton();
    this.warning = new javax.swing.JLabel();
    this.jPanel3 = new javax.swing.JPanel();
    this.jButton2 = new javax.swing.JButton();
    this.jButton3 = new javax.swing.JButton();
    this.jButton4 = new javax.swing.JButton();
    this.jButton5 = new javax.swing.JButton();
    
    setDefaultCloseOperation(0);
    setMinimumSize(new java.awt.Dimension(314, 452));
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        Blacklist.this.formWindowClosing(evt);
      }
      
    });
    this.jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("黑名单列表"));
    
    this.jScrollPane1.setViewportView(this.guiBlacklist);
    
    GroupLayout jPanel1Layout = new GroupLayout(this.jPanel1);
    this.jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(this.jScrollPane1, -1, 213, 32767));
    


    jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(this.jScrollPane1, -1, 323, 32767));
    



    getContentPane().add(this.jPanel1, "Center");
    
    this.jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("添加"));
    
    this.jTextField1.setText("在此输入ip");
    this.jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        Blacklist.this.jTextField1MousePressed(evt);
      }
      
    });
    this.jButton1.setText("添加");
    this.jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        Blacklist.this.jButton1ActionPerformed(evt);
      }
      
    });
    GroupLayout jPanel2Layout = new GroupLayout(this.jPanel2);
    this.jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup().addComponent(this.jTextField1, -1, 214, 32767).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.jButton1).addGap(9, 9, 9)));
    






    jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(this.jTextField1, -2, -1, -2).addComponent(this.jButton1)));
    





    getContentPane().add(this.jPanel2, "First");
    
    this.warning.setHorizontalAlignment(2);
    this.warning.setText(" 状态栏  ");
    getContentPane().add(this.warning, "Last");
    
    this.jButton2.setText("删除");
    this.jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        Blacklist.this.jButton2ActionPerformed(evt);
      }
      
    });
    this.jButton3.setText("清空");
    this.jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        Blacklist.this.jButton3ActionPerformed(evt);
      }
      
    });
    this.jButton4.setText("重载");
    this.jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        Blacklist.this.jButton4ActionPerformed(evt);
      }
      
    });
    this.jButton5.setText("关闭");
    this.jButton5.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        Blacklist.this.jButton5ActionPerformed(evt);
      }
      
    });
    GroupLayout jPanel3Layout = new GroupLayout(this.jPanel3);
    this.jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createSequentialGroup().addContainerGap(-1, 32767).addComponent(this.jButton5).addContainerGap()).addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(this.jButton3, javax.swing.GroupLayout.Alignment.TRAILING).addComponent(this.jButton4, javax.swing.GroupLayout.Alignment.TRAILING).addComponent(this.jButton2, javax.swing.GroupLayout.Alignment.TRAILING)).addContainerGap(-1, 32767)));
    












    jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup().addContainerGap(220, 32767).addComponent(this.jButton2).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jButton4).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jButton3).addGap(18, 18, 18).addComponent(this.jButton5).addContainerGap()));
    












    getContentPane().add(this.jPanel3, "After");
    
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width - 314) / 2, (screenSize.height - 452) / 2, 314, 452);
  }
  
  private void jTextField1MousePressed(java.awt.event.MouseEvent evt)
  {
    this.jTextField1.setText("");
  }
  
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
  {
    String tmpIp = this.jTextField1.getText().trim();
    this.jTextField1.setText("在此输入ip");
    if ((tmpIp == null) || (tmpIp.equals(""))) {
      this.warning.setText("请输入ip地址！");
      return;
    }
    if (!ipmsg.etc.UtilityGlobal.isIP(tmpIp)) {
      this.warning.setText("请输入正确的ip地址！");
      return;
    }
    if (ipmsg.etc.GlobalVar.BLACK_LIST.contains(tmpIp)) {
      this.warning.setText("该IP地址已经存在于黑名单！");
      return;
    }
    if (ipmsg.etc.GlobalVar.ALL_IP_ADDRESS.contains(tmpIp)) {
      this.warning.setText("该ip属于本地ip，不能执行此操作！");
      return;
    }
    ipmsg.etc.GlobalVar.BLACK_LIST.add(tmpIp);
    refreshGui("将ip \"" + tmpIp + " \"添加至黑名单！");
  }
  
  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
  {
    if (this.guiBlacklist.getSelectedValues().length == 0) {
      this.warning.setText("请选择黑名单记录！");
      return;
    }
    
    int[] tmpSelected = this.guiBlacklist.getSelectedIndices();
    for (int i = tmpSelected.length - 1; i >= 0; i--) {
      ipmsg.etc.GlobalVar.BLACK_LIST.remove(tmpSelected[i]);
    }
    refreshGui("一共将 " + tmpSelected.length + " 个黑名单移除");
  }
  
  private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
  {
    if (!UtilityGui.confirm(this, "清空黑名单", "您确定要清空黑名单列表？")) {
      return;
    }
    ipmsg.etc.GlobalVar.BLACK_LIST.clear();
    refreshGui("您已经清空了黑名单");
  }
  
  private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
  {
    refreshGui("当前共有 " + ipmsg.etc.GlobalVar.BLACK_LIST.size() + " 个黑名单");
  }
  
  private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)
  {
    exit();
  }
  
  private void formWindowClosing(java.awt.event.WindowEvent evt)
  {
    exit();
  }
  


  public static void main(String[] args)
  {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new Blacklist().setVisible(true);
      }
    });
  }
  














  private void refreshGui(String str)
  {
    this.guiBlacklist.setListData(ipmsg.etc.GlobalVar.BLACK_LIST);
    this.warning.setText(str);
  }
  
  private void exit()
  {
    ipmsg.etc.GlobalVar.WINDOW_REG.remove("blacklist");
    dispose();
  }
}
